/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mantechhelpdesk.bean;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import mantechhelpdesk.dao.AnswersDao;
import mantechhelpdesk.dao.ComplaintsDao;
import mantechhelpdesk.dao.DbConnection;
import mantechhelpdesk.dao.DepartmentsDao;
import mantechhelpdesk.dao.ThreadsDao;
import mantechhelpdesk.dao.UsersDao;
import mantechhelpdesk.entity.Answers;
import mantechhelpdesk.entity.Complaints;
import mantechhelpdesk.entity.Departments;
import mantechhelpdesk.entity.Threads;
import mantechhelpdesk.entity.Users;

/**
 *
 * @author MANHDD
 */
public class ComplaintsBean {

    private Complaints complaints = null;
    private Answers answers = null;
    private List<Complaints> complaintsList;
    private List<SelectItem> departmentsList;
    private DepartmentsDao departmentsDao;
    private List<SelectItem> threadsList;
    private ThreadsDao threadsDao;
    private List<SelectItem> usersList;
    private UsersDao usersDao;
    private ComplaintsDao complaintsDao;
    private Connection conn;

    public Complaints getComplaints() {
        return complaints;
    }

    public void setComplaints(Complaints complaints) {
        this.complaints = complaints;
    }

    public List<Complaints> getComplaintsList() {
        return complaintsList;
    }

    public void setComplaintsList(List<Complaints> complaintsList) {
        this.complaintsList = complaintsList;
    }

    public List<SelectItem> getDepartmentsList() {
        return departmentsList;
    }

    public void setDepartmentsList(List<SelectItem> departmentsList) {
        this.departmentsList = departmentsList;
    }

    public List<SelectItem> getThreadsList() {
        return threadsList;
    }

    public void setThreadsList(List<SelectItem> threadsList) {
        this.threadsList = threadsList;
    }

    public List<SelectItem> getUsersList() {
        return usersList;
    }

    public void setUsersList(List<SelectItem> usersList) {
        this.usersList = usersList;
    }

    public Answers getAnswers() {
        return answers;
    }

    public void setAnswers(Answers answers) {
        this.answers = answers;
    }

    public ComplaintsBean() {

        try {
            complaintsDao = new ComplaintsDao();
            departmentsDao = new DepartmentsDao();
            threadsDao = new ThreadsDao();
            usersDao = new UsersDao();
            complaints = new Complaints();
            answers = new Answers();
            conn = DbConnection.getConnection();

            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = request.getSession(true);
            Object attribute = session.getAttribute("HelpDeskLogin");

            complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), true);

            departmentsList = new ArrayList();
            List allDepart = departmentsDao.loadAll(conn);
            Iterator it = allDepart.iterator();
            while (it.hasNext()) {
                Departments departments = (Departments) it.next();
                departmentsList.add(new SelectItem(departments.getDepartmentID(), departments.getDepartmentName()));
            }

            threadsList = new ArrayList();
            List allThread = threadsDao.loadAll(conn);
            Iterator at = allThread.iterator();
            while (at.hasNext()) {
                Threads threads = (Threads) at.next();
                threadsList.add(new SelectItem(threads.getThreadID(), threads.getThreadName()));
            }

            usersList = new ArrayList();
            List allUser = usersDao.loadAll(conn);
            Iterator et = allUser.iterator();
            while (et.hasNext()) {
                Users users = (Users) et.next();
                usersList.add(new SelectItem(users.getUserID(), users.getUsername()));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String addNew() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (complaints.getComplaintContent().length() > 0) {

                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                HttpSession session = request.getSession(true);
                Object attribute = session.getAttribute("HelpDeskLogin");

                Calendar now = GregorianCalendar.getInstance();
                complaints.setCreateTime(now.getTime());
                complaints.setUserID(Integer.parseInt(attribute.toString()));
                complaints.setFinishedTime(now.getTime());
                complaints.setHasReplied(false);
                complaints.setIsFinished(false);
                complaints.setUserRef(2);
                complaintsDao.create(conn, complaints);
                complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), true);
                ct.addMessage("", new FacesMessage("Complaintment addnew Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Complaintment name!"));
                return "addNewError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot addnew Complaintment!"));
        }
        return "complaintAddBack";
    }

    public String addNewAnswers() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (answers.getAnswerContent().length() > 0) {
                AnswersDao answersDao = new AnswersDao();
                Calendar now = GregorianCalendar.getInstance();
                answers.setCreateTime(now.getTime());
                answersDao.create(conn, answers);

                //Load data
                complaints = complaintsDao.getObject(conn, answers.getComplaintID());
                complaints.setHasReplied(true);
                complaints.setIsFinished(true);
                complaints.setFinishedTime(now.getTime());
                complaintsDao.save(conn, complaints);
                answers = answersDao.getAnswersByComplaintId(conn, answers.getComplaintID());
                ct.addMessage("", new FacesMessage("Answer addnew Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Answer name!"));
                return "addNewError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot addnew Answer!"));
        }
        return "addNewSuccess";
    }

    public String update() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (complaints.getComplaintContent().length() > 0) {
                HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                HttpSession session = request.getSession(true);
                Object attribute = session.getAttribute("HelpDeskLogin");

                complaintsDao.save(conn, complaints);
                complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), true);
                ct.addMessage("", new FacesMessage("Complaintment update Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Complaintment name!"));
                return "departUpdateError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot update Complaintment!"));
        }
        return "updateBackToComplaintList";
    }

    public String delete() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = request.getSession(true);
            Object attribute = session.getAttribute("HelpDeskLogin");

            complaintsDao.delete(conn, complaints);
            ct.addMessage("", new FacesMessage("Complaintment delete Success!"));
            complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), true);
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot delete Complaintment!"));
        }
        return "delBackToComplaintList";
    }

    public String prepareData4AddAnswers() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            answers.setComplaintID(Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "detailComplaint";
    }

    public String prepareData4Update() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints = complaintsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "updateComplaint";
    }

    public String prepareData4AddComplaint() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints.setUserID(Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "complaintAddNew";
    }

    public String prepareData4DetailAnsBack() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints = complaintsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "addNewSuccess";
    }

    public String prepareData4DetailAnsUpdateBack() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            AnswersDao answersDao = new AnswersDao();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints = complaintsDao.getObject(conn, Integer.parseInt(id));
            answers = answersDao.getAnswersByComplaintId(conn, complaints.getComplaintID());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "updateSuccess";
    }

    public String prepareData4ListReplied() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = request.getSession(true);
            Object attribute = session.getAttribute("HelpDeskLogin");
            complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "complaintListReplied";
    }

    public String prepareData4ListNotReplied() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = request.getSession(true);
            Object attribute = session.getAttribute("HelpDeskLogin");
            complaintsList = complaintsDao.loadAll(conn, Integer.parseInt(attribute.toString()), false);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "complaintListReplied";
    }

    public String prepareData4Detail() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            AnswersDao answersDao = new AnswersDao();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints = complaintsDao.getObject(conn, Integer.parseInt(id));
            answers = answersDao.getAnswersByComplaintId(conn, complaints.getComplaintID());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "detailComplaint";
    }

    public String prepareData4Delete() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            complaints = complaintsDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "delComplaint";
    }
}
